#include <QtGui/QApplication>
#include "./header/pulldata.h"
#include "./header/graph.h"
#include "predictor.h"
#include "stock.h"
#include "randomgenerator.h"
#include <QFile>
#include <QHash>
#include <QHashIterator>
#include <QtCore>
#include <QObject>
#include <sleeper.h>
#include <QTimer>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    const int screenWidth = QApplication::desktop()->width();
    const int screenHeight = QApplication::desktop()->height();
    qDebug() << "Screen width = " << screenWidth << " and Screen Height = " << screenHeight;
    PullData mw;
    mw.setGeometry(100, 200, mw.geometry().width(), mw.geometry().height());
    mw.show();
    QDateTime current = QDateTime::currentDateTime();
    QDateTime startingDemo = current.addSecs(20); //Getting data for like 60 secs

    while (1) {
        if (current.toMSecsSinceEpoch() < startingDemo.toMSecsSinceEpoch()) {
            qDebug() << "Fetching data";
            Sleeper::sleep(2);
            current = current.addSecs(5);
            current.setTime(current.time());
            mw.getHttpGetData();
        }
        else {
            break;
        }
    }

    graph *g = new graph;
    bool status;
//    We don't use Random Generator.
//    RandomGenerator *rg = new RandomGenerator;
//    bool status = QObject::connect(g, SIGNAL(stockValueChanged(QString)), rg, SLOT(receiveChangeFromUI(QString)));
//    qDebug() << "1. Status = " << status;
//    if (!status) {
//        return a.exec();
//    }

    g->setStockCode(graph::googleStockCode);

    Stock *s = new Stock(graph::googleDataFile, graph::googleStockCode);
    g->setStockData(s);
    g->cache.insert(graph::googleStockCode, s);
    Stock *t = new Stock(graph::appleDataFile, graph::appleStockCode);
    g->cache.insert(graph::appleStockCode, t);
    Stock *u = new Stock(graph::microsoftDataFile, graph::microsoftStockCode);
    g->cache.insert(graph::microsoftStockCode, u);
    status = QObject::connect(s, SIGNAL(sendSignalToPaintEvent()), g, SLOT(repaint()));
    qDebug() << "2. Status = " << status;
    if (!status) {
        return a.exec();
    }
    g->show();

    Predictor *p = new Predictor(5);
    p->setHighestStockValue(s->getHighestStockReading());
    status = QObject::connect(p, SIGNAL(drawNextLineToUi(QHash<QString, QString>)), g, SLOT(predictionData(QHash<QString, QString>)));
    qDebug() << "3. Status = " << status;
    if (!status) {
        return a.exec();
    }
    p->setLastDataPredicted(s->getLastLineRead());
    g->setPredictor(p);
    g->setPredictionTimer("10"); //10 secs default
    g->setConnectionOfTimer();
    g->startPredictionTimer();

    return a.exec();
}


